package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPoll {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();

    public MyThreadPoll(int n) {
        // 在构造方法中创建线程
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                while (true) {
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}

public class ThreadDemo27 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoll poll = new MyThreadPoll(10);
        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            poll.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello " + finalI);
                }
            });
        }
    }
}
